<?php
require("platby-top.dat");
?>

<div class="content">
<?php
	if((is_numeric($_POST["den"])) && (is_numeric($_POST["mesic"])) && (is_numeric($_POST["rok"])) && (is_numeric($_POST["castka"])) && (is_numeric($_POST["vs"]))) {
		$date=$_POST["rok"]."-".$_POST["mesic"]."-".$_POST["den"];
		
		mysql_query("INSERT INTO platby (datum, castka, vs, poznamka) VALUES ('".$date."', ".$_POST["castka"].", '".$_POST["vs"]."', '".$_POST["note"]."')")
			or die(mysql_error());
		$result = mysql_query("SELECT p_id FROM platby WHERE datum='".$date."' AND castka=".$_POST["castka"]." AND vs='".$_POST["vs"]."'") 
			or die(mysql_error());
		$row = mysql_fetch_assoc($result);
		$p_id = $row["p_id"];
		$result = mysql_query("SELECT * FROM ucast WHERE u_vs='".$_POST["vs"]."'")
			or die(mysql_error());
		if(mysql_num_rows($result)==1) { //platba namatchovana na jednoho ucastnika
			$row=mysql_fetch_assoc($result);
			$result2 = mysql_query("SELECT zapisne FROM kategorie WHERE kat='".$row["kategorie"]."'")
				or die(mysql_error());
			$row2=mysql_fetch_assoc($result2);
			$zapisne=$row2["zapisne"];
			if($zapisne==$_POST["castka"]) { //sedi penize
				mysql_query("UPDATE ucast SET stav=1 WHERE u_id=".$row["u_id"]) or die(mysql_error());
				mysql_query("UPDATE platby SET stav=1 WHERE p_id=".$p_id) or die(mysql_error());
				echo("Platba za ucastnika ".$row["nick"]." odpovida.<br />");
			} else { //nesedi penize
				mysql_query("UPDATE ucast SET stav=2 WHERE u_id=".$row["u_id"]) or die(mysql_error());
				mysql_query("UPDATE platby SET stav=2 WHERE p_id=".$p_id) or die(mysql_error());
				echo("Platba za ucastnika ".$row["nick"]." nesedi.<br />");
				echo("Zapisne je ".$zapisne." a castka ".$_POST["castka"].".<br />");
				echo("Mail na ".$row["nick"]." je ".$row["email"].".<br />");
			}
		} else {
			$result3 = mysql_query("SELECT * FROM vs WHERE new_vs='".$_POST["vs"]."'")
				or die(mysql_error());
			if(mysql_num_rows($result3)>=1) { //platba namatchovana na skupinovy vs
				$zapisne=0;
				while($row3 = mysql_fetch_assoc($result3)) { //scitam zapisne pro jednotlive lidi...
					$result = mysql_query("SELECT * FROM ucast WHERE u_vs='".$row3["u_vs"]."'")
						or die(mysql_error());
					$row=mysql_fetch_assoc($result);
					$result2 = mysql_query("SELECT zapisne FROM kategorie WHERE kat='".$row["kategorie"]."'")
						or die(mysql_error());
					$row2=mysql_fetch_assoc($result2);
					$zapisne += $row2["zapisne"];
				}
				if($zapisne==$_POST["castka"]) { //sedi penize
					//nastavim jednotlivim lidem, ze sou ok
					$result3 = mysql_query("SELECT * FROM vs WHERE new_vs='".$vs."'")
						or die(mysql_error());
					while($row3 = mysql_fetch_assoc($result3)) {
						mysql_query("UPDATE ucast SET stav=1 WHERE u_vs='".$row3["u_vs"]."'") or die(mysql_error());
					}
					//platba je ok
					mysql_query("UPDATE platby SET stav=1 WHERE p_id=".$p_id) or die(mysql_error());
					echo("Platba za vsechny ucastniky pod danym vs odpovida.<br />");
				} else {
					$result3 = mysql_query("SELECT * FROM vs WHERE new_vs='".$vs."'")
						or die(mysql_error());
					while($row3 = mysql_fetch_assoc($result3)) {
						mysql_query("UPDATE ucast SET stav=2 WHERE u_vs=".$row3["u_vs"]) or die(mysql_error());
					}
					mysql_query("UPDATE platby SET stav=3 WHERE p_id=".$p_id) or die(mysql_error());
					echo("Nesedi castka.<br />");
					echo("Zapisne za ucastniky pod dany vs je ".$zapisne." a castka ".$_POST["castka"].".<br />");
				}
			} else {
				mysql_query("UPDATE platby SET stav=2 WHERE p_id=".$p_id) or die(mysql_error());
				echo("Variabilni symbol ".$_POST["vs"]."nemame v db.<br />");
			}
		}
		
		//lidi by meli byt...
	} else {
		echo("Vsechno krom poznamky musi byt cisla!<br />");
	}
	
?>
	<a href="novaplatba.php">Zadat dalsi platbu</a><br />
</div>

<?php
require("platby-end.dat");
?>
